package cn.aj.pet.controller;

import cn.aj.pet.dto.PublishDto;
import cn.aj.pet.service.ISearchMasterMsgService;
import cn.aj.pet.domain.SearchMasterMsg;
import cn.aj.pet.query.SearchMasterMsgQuery;
import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.PageListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResultUtil addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageListUtil<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.loadPage(query);
    }

    /**
    * @Title: publish
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/7 0007 15:35
    * @Parameters: [query]
    * @Return cn.aj.basic.utils.PageListUtil<cn.aj.pet.domain.SearchMasterMsg>
     * 宠物发布寻主
    */
    @PostMapping("/publish")
    public AjaxResultUtil publish(@RequestBody PublishDto publishDto)
    {
        searchMasterMsgService.publish(publishDto);
        return AjaxResultUtil.getAjaxResult().success();
    }
}
